package com.wtx.chapter04;

/**
 * @description:
 * @author: wtx
 * @createDate: 2020/6/10
 */
public class BinarySearch {

    public static int search(int[] array, int k){

        int l = 0;
        int r = array.length-1;
        int mid = 0;
        while (l<=r){

            mid = (l+r)/2;
            if (k==array[mid])
                return mid;
            else if (k<array[mid])
                r = mid-1;
            else
                l = mid+1;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] sorted = {1,2,3,4,5};
        int search = BinarySearch.search(sorted, 2);
        System.out.println(search);
    }
}
